#include "SkillProxyModel.h"



#include <ItemModel.h>
#include <ItemReference.h>



#include <QDebug>



namespace
{
QString SKILL_GROUP_ID = ".Skill";
QString ATTRIBUTE_GROUP_ID = ".Attribute";
}



SkillProxyModel::SkillProxyModel(QObject *parent) :
    AbstractTreeProxyModel(parent)
{
}



int SkillProxyModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);

    return (Field_LAST - Field_FIRST + 1);
}



QVariant SkillProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    Q_UNUSED(orientation);
    if (role != Qt::DisplayRole) return QVariant();
    switch (section)
    {
    case NameField: return tr("Skill");
    case RefField:  return tr("Attribute");
    }

    return QVariant();
}



void SkillProxyModel::remap()
{
    clear();

    ItemModel* model = dynamic_cast<ItemModel*>(sourceModel());
    if (!model) return;

    QModelIndex groupIndex = model->index(0, AbstractItem::IdData, QModelIndex());
    while (groupIndex.isValid())
    {
        if (groupIndex.data(Qt::EditRole).toString().contains(SKILL_GROUP_ID)) break;

        groupIndex = groupIndex.sibling(groupIndex.row()+1, AbstractItem::IdData);
    }

    if (!groupIndex.isValid()) return;

    emit layoutAboutToBeChanged();

    QModelIndex skillIndex = groupIndex.child(0, AbstractItem::IdData);
    while (skillIndex.isValid())
    {
        mapSkillIndex(skillIndex);

        skillIndex = skillIndex.sibling(skillIndex.row()+1, AbstractItem::IdData);
    }


    emit layoutChanged();
}



void SkillProxyModel::mapSkillIndex(const QModelIndex& index)
{
    if (!index.isValid()) return;

    QModelIndex nameIndex = index.sibling(index.row(), AbstractItem::NameData);
    if (!nameIndex.isValid()) return;

    QModelIndex refIndex = index.child(0, ItemReference::RefData);
    while (refIndex.isValid())
    {
        if (!refIndex.data(Qt::EditRole).toString().contains(ATTRIBUTE_GROUP_ID))
        {
            refIndex = refIndex.sibling(refIndex.row()+1, ItemReference::RefData);
            continue;
        }

        QModelIndex refTypeIndex = index.sibling(refIndex.row(), ItemReference::TypeData);

        if (!refTypeIndex.isValid() || refTypeIndex.data(Qt::EditRole).toInt() != ItemReference::BonusRef)
        {
            refIndex = refIndex.sibling(refIndex.row()+1, ItemReference::RefData);
            continue;
        }

        break;
    }

    //qDebug(QString("%1, %2").arg(nameIndex.data(Qt::DisplayRole).toString()).arg(refIndex.data(Qt::DisplayRole).toString()).toAscii());

    addMapping(nameIndex, createIndex(nameIndex.row(), NameField, refIndex.internalPointer()));
    addMapping(refIndex, createIndex(nameIndex.row(), RefField, refIndex.internalPointer()));

    mapSubIndexes(nameIndex);
}



void SkillProxyModel::mapSubIndexes(const QModelIndex &index)
{
    //qDebug() << Q_FUNC_INFO;

    if (!index.isValid()) return;


    QModelIndex parentProxy = mapFromSource(index);
    if (!parentProxy.isValid()) return;


    ItemModel* model = dynamic_cast<ItemModel*>(sourceModel());
    if (!model) return;


    QModelIndex refIndex = index.child(0, ItemReference::RefData);

    while (refIndex.isValid())
    {
        QModelIndex refTypeIndex = refIndex.sibling(refIndex.row(), ItemReference::TypeData);

        if (!refTypeIndex.isValid())
        {
            refIndex = refIndex.sibling(refIndex.row()+1, ItemReference::RefData);
            continue;
        }

        int type = refTypeIndex.data(Qt::EditRole).toInt();
        if (type != ItemReference::GroupRef)
        {
            refIndex = refIndex.sibling(refIndex.row()+1, ItemReference::RefData);
            continue;
        }

        break;
    }

    if (!refIndex.isValid()) return;

    //qDebug() << "Found subskill group reference:" << refIndex.data();

    QModelIndexList subIndexes = model->references(refIndex);
    foreach (QModelIndex subIndex, subIndexes)
    {
        if (!subIndex.isValid()) continue;

        QModelIndex nameIndex = subIndex.sibling(subIndex.row(), AbstractItem::NameData);
        QModelIndex idIndex = subIndex.sibling(subIndex.row(), AbstractItem::IdData);

        if (!nameIndex.isValid() || !idIndex.isValid()) continue;

        addChildMapping(parentProxy, nameIndex, createIndex(subIndex.row(), NameField, nameIndex.internalPointer()));
        addChildMapping(parentProxy, idIndex, createIndex(subIndex.row(), RefField, idIndex.internalPointer()));
    }

    //qDebug() << "Added" << m_children.value(parentProxy).count()/2 << "sub skills for" << index.data().toString();

}
